package com.example.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 多业务Redis配置
 * 
 * @author AI Assistant
 * @since 2025-01-08
 */
@Configuration
public class MultiRedisConfig {

    /**
     * 用户缓存Redis连接工厂
     */
    @Primary
    @Bean(name = "userCacheRedisConnectionFactory")
    @ConfigurationProperties(prefix = "spring.redis.user-cache")
    public LettuceConnectionFactory userCacheRedisConnectionFactory() {
        return new LettuceConnectionFactory();
    }

    /**
     * 会话Redis连接工厂
     */
    @Bean(name = "sessionRedisConnectionFactory")
    @ConfigurationProperties(prefix = "spring.redis.session")
    public LettuceConnectionFactory sessionRedisConnectionFactory() {
        return new LettuceConnectionFactory();
    }

    /**
     * 消息队列Redis连接工厂
     */
    @Bean(name = "messageQueueRedisConnectionFactory")
    @ConfigurationProperties(prefix = "spring.redis.message-queue")
    public LettuceConnectionFactory messageQueueRedisConnectionFactory() {
        return new LettuceConnectionFactory();
    }

    /**
     * 用户缓存Redis模板
     */
    @Primary
    @Bean(name = "userCacheRedisTemplate")
    public RedisTemplate<String, Object> userCacheRedisTemplate(
            @Qualifier("userCacheRedisConnectionFactory") RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        
        // 设置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        
        template.afterPropertiesSet();
        return template;
    }

    /**
     * 会话Redis模板
     */
    @Bean(name = "sessionRedisTemplate")
    public RedisTemplate<String, Object> sessionRedisTemplate(
            @Qualifier("sessionRedisConnectionFactory") RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        
        // 设置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        
        template.afterPropertiesSet();
        return template;
    }

    /**
     * 消息队列Redis模板
     */
    @Bean(name = "messageQueueRedisTemplate")
    public RedisTemplate<String, Object> messageQueueRedisTemplate(
            @Qualifier("messageQueueRedisConnectionFactory") RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        
        // 设置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        
        template.afterPropertiesSet();
        return template;
    }
}
